package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.enums.PopupPosition;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.AlbumActivity;
import flc.ast.adapter.AlbumAdapter;
import flc.ast.bean.MyScaleBean;
import flc.ast.databinding.FragmentAlbumManageBinding;
import flc.ast.dialog.AddDialog;
import flc.ast.dialog.DeleteDialog;
import flc.ast.dialog.EditDialog;
import flc.ast.dialog.RenameDialog;
import flc.ast.dialog.ScaleDialog;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link AlbumManageFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class AlbumManageFragment extends BaseNoModelFragment<FragmentAlbumManageBinding> {

    private AlbumAdapter albumAdapter;
    private List<String> listShow = new ArrayList<>();//全部数据
    private int selPosition;
    private BasePopupView editPopup;
    private EditDialog editDialog;

    @Override
    protected int onCreate() {
        return R.layout.fragment_album_manage;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivAdd.setOnClickListener(this);

        mDataBinding.rvAlbumList.setLayoutManager(new LinearLayoutManager(mContext));
        albumAdapter = new AlbumAdapter();
        mDataBinding.rvAlbumList.setAdapter(albumAdapter);
        albumAdapter.addChildClickViewIds(R.id.ivAlbumItemEdit);
        albumAdapter.setOnItemClickListener(this);
        albumAdapter.setOnItemChildClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivAdd://添加
                showAddDialog();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        AlbumActivity.sFilePath = FileUtils.getFileName(albumAdapter.getItem(position));
        startActivity(AlbumActivity.class);
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivAlbumItemEdit://编辑
                selPosition = position;
                showEditDialog(view);
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        listShow.clear();
        String path = PathUtils.getFilesPathExternalFirst() + Content.myAlbum;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(file.getPath());
            }
        }
        if (listShow.size() > 0) {
            albumAdapter.setList(listShow);
            mDataBinding.rvAlbumList.setVisibility(View.VISIBLE);
            mDataBinding.tvAlbumNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvAlbumList.setVisibility(View.GONE);
            mDataBinding.tvAlbumNoData.setVisibility(View.VISIBLE);
        }
    }

    private void showEditDialog(View view) {
        if (editDialog == null) editDialog = new EditDialog(mContext);
        editPopup = new XPopup.Builder(mContext).popupPosition(PopupPosition.Right) //手动指定弹窗的位置
                .isDestroyOnDismiss(false) //对于只使用一次的弹窗，推荐设置这个
                .atView(view).hasShadowBg(false) // 去掉半透明背景
                .asCustom(editDialog);

        editDialog.setListener(new EditDialog.Ilistener() {
            @Override
            public void clickRename() {
                showRenameDialog();
            }

            @Override
            public void clickDelete() {
                showDeleteDialog();
            }
        });

        editPopup.show();
    }

    private void showAddDialog() {
        AddDialog dialog = new AddDialog(mContext);
        dialog.setListener(new AddDialog.Ilistener() {
            @Override
            public void clickAddAlbum(String name) {
                String path = PathUtils.getFilesPathExternalFirst() + Content.myAlbum + "/" + name;
                if (FileUtils.isFileExists(path)) {
                    ToastUtils.showShort(getString(R.string.album_is_exist));
                } else {
                    FileUtils.createOrExistsDir(path);
                    ToastUtils.showShort(getString(R.string.add_album_suc));
                }
                getData();
            }
        });
        dialog.show();
    }

    private void showRenameDialog() {
        RenameDialog dialog = new RenameDialog(mContext);
        dialog.setListener(new RenameDialog.Ilistener() {
            @Override
            public void clickRename(String name) {
                FileUtils.rename(listShow.get(selPosition), name);
                getData();
            }
        });
        dialog.show();
    }

    private void showDeleteDialog() {
        DeleteDialog dialog = new DeleteDialog(mContext);
        dialog.setListener(new DeleteDialog.Ilistener() {
            @Override
            public void clickDelete() {
                //从此文件夹删除
                FileUtils.delete(listShow.get(selPosition));
                ToastUtils.showShort(getString(R.string.delete_suc));
                getData();
            }
        });
        dialog.show();
    }

}